package com.ljkj.lawyer.lawyer.service;


import com.ljkj.lawyer.lawyer.common.MessageResult;
import com.ljkj.lawyer.lawyer.common.RestResult;
import com.ljkj.lawyer.lawyer.dao.CommerceDao;
import com.ljkj.lawyer.lawyer.dao.LawyerDao;
import com.ljkj.lawyer.lawyer.entity.CommerceEntity;
import com.ljkj.lawyer.lawyer.entity.LawyerAddRecordEntity;
import com.ljkj.lawyer.lawyer.entity.LawyerEntity;
import com.ljkj.lawyer.lawyer.param.HttpMemberRes;
import com.ljkj.lawyer.lawyer.param.card.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;

@Service
public class CardService {
    @Autowired
    private LawyerDao lawyerDao;

    @Autowired
    private HttpService httpService;

    @Autowired
    private CommerceDao commerceDao;

    public RestResult getLawyerList(Integer pageNum, Integer pageSize) {
        LawyerListRes lawyerListRes = new LawyerListRes();
        List<LawyerRes> lawyerResList = new ArrayList<>();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<LawyerEntity> pageResult = lawyerDao.getLawyerList(pageable);
        List<LawyerEntity> lawyerEntityList = pageResult.getContent();
        lawyerListRes.setPageNum(pageNum);
        lawyerListRes.setPageSize(pageSize);
        lawyerListRes.setTotal(pageResult.getTotalElements());
        lawyerListRes.setPages(pageResult.getTotalPages());
        if(!CollectionUtils.isEmpty(lawyerEntityList)){
            lawyerEntityList.forEach(p -> {
                LawyerRes lawyerRes = new LawyerRes();
                HttpMemberRes httpMemberRes = httpService.getMember(p.getMemberId());
                if (null != httpMemberRes) {
                    lawyerRes.setUsername(httpMemberRes.getUsername());
                    lawyerRes.setPhone(httpMemberRes.getMobilePhone());
                }
                BeanUtils.copyProperties(p,lawyerRes);
                lawyerResList.add(lawyerRes);
            });
        }
        lawyerListRes.setLawyerResList(lawyerResList);
        return RestResult.success(lawyerListRes);
    }
    public RestResult searchLawyerList(SearchParam searchParam){
        LawyerListRes lawyerListRes = new LawyerListRes();
        List<LawyerRes> lawyerResList = new ArrayList<>();
        Pageable pageable = PageRequest.of(searchParam.getPageNum() - 1, searchParam.getPageSize());
        Page<LawyerEntity> pageResult = lawyerDao.searchLawyerList(searchParam.getMemberId(),pageable);
        List<LawyerEntity> lawyerEntityList = pageResult.getContent();
        lawyerListRes.setPageNum(searchParam.getPageNum());
        lawyerListRes.setPageSize(searchParam.getPageSize());
        lawyerListRes.setTotal(pageResult.getTotalElements());
        lawyerListRes.setPages(pageResult.getTotalPages());
        if(!CollectionUtils.isEmpty(lawyerEntityList)){
            lawyerEntityList.forEach(p -> {
                LawyerRes lawyerRes = new LawyerRes();
                HttpMemberRes httpMemberRes = httpService.getMember(p.getMemberId());
                if (null != httpMemberRes) {
                    lawyerRes.setUsername(httpMemberRes.getUsername());
                    lawyerRes.setPhone(httpMemberRes.getMobilePhone());
                }
                BeanUtils.copyProperties(p,lawyerRes);
                lawyerResList.add(lawyerRes);
            });
        }
        lawyerListRes.setLawyerResList(lawyerResList);
        return RestResult.success(lawyerListRes);
    }

    public RestResult getCommerceList(Integer pageNum, Integer pageSize) {
        CommerceListRes commerceListRes = new CommerceListRes();
        List<CommerceRes> commerceResList = new ArrayList<>();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        Page<CommerceEntity> pageResult = commerceDao.getCommerceList(pageable);
        List<CommerceEntity> commerceEntityList = pageResult.getContent();
        commerceListRes.setPageNum(pageNum);
        commerceListRes.setPageSize(pageSize);
        commerceListRes.setTotal(pageResult.getTotalElements());
        commerceListRes.setPages(pageResult.getTotalPages());
        if(!CollectionUtils.isEmpty(commerceEntityList)){
            commerceEntityList.forEach(p -> {
                CommerceRes commerceRes = new CommerceRes();
                HttpMemberRes httpMemberRes = httpService.getMember(p.getMemberId());
                if (null != httpMemberRes) {
                    commerceRes.setUsername(httpMemberRes.getUsername());
                    commerceRes.setPhone(httpMemberRes.getMobilePhone());
                }
                BeanUtils.copyProperties(p,commerceRes);
                commerceResList.add(commerceRes);
            });
        }
        commerceListRes.setCommerceResList(commerceResList);
        return RestResult.success(commerceListRes);
    }

    public RestResult searchCommerceList(SearchParam searchParam){
        CommerceListRes commerceListRes = new CommerceListRes();
        List<CommerceRes> commerceResList = new ArrayList<>();
        Pageable pageable = PageRequest.of(searchParam.getPageNum() - 1, searchParam.getPageSize());
        Page<CommerceEntity> pageResult = new Page<CommerceEntity>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public <U> Page<U> map(Function<? super CommerceEntity, ? extends U> function) {
                return null;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<CommerceEntity> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<CommerceEntity> iterator() {
                return null;
            }
        };
        if(searchParam.getMemberId() !=null){
            pageResult = commerceDao.searchCommerceListByMemberId(searchParam.getMemberId(),pageable);
        }else{
            if(!StringUtils.isEmpty(searchParam.getKeyWord())){
                pageResult = commerceDao.searchCommerceListByKeyWord(searchParam.getKeyWord(),pageable);
            }
        }
        List<CommerceEntity> commerceEntityList = pageResult.getContent();
        commerceListRes.setPageNum(searchParam.getPageNum());
        commerceListRes.setPageSize(searchParam.getPageSize());
        commerceListRes.setTotal(pageResult.getTotalElements());
        commerceListRes.setPages(pageResult.getTotalPages());
        if(!CollectionUtils.isEmpty(commerceEntityList)){
            commerceEntityList.forEach(p -> {
                CommerceRes commerceRes = new CommerceRes();
                HttpMemberRes httpMemberRes = httpService.getMember(p.getMemberId());
                if (null != httpMemberRes) {
                    commerceRes.setUsername(httpMemberRes.getUsername());
                    commerceRes.setPhone(httpMemberRes.getMobilePhone());
                }
                BeanUtils.copyProperties(p,commerceRes);
                commerceResList.add(commerceRes);
            });
        }
        commerceListRes.setCommerceResList(commerceResList);
        return RestResult.success(commerceListRes);
    }

    public RestResult getCommerceByMemberId(Integer memberId) {
        CommerceDetailRes commerceDetailRes =new CommerceDetailRes();
        CommerceEntity commerceEntity = commerceDao.findByMemberId(memberId);
        if(null != commerceEntity){
            BeanUtils.copyProperties(commerceEntity,commerceDetailRes);
        }
        HttpMemberRes httpMemberRes = httpService.getMember(memberId);
        if (null != httpMemberRes) {
            commerceDetailRes.setAvatar(httpMemberRes.getAvatar());
            commerceDetailRes.setUsername(httpMemberRes.getUsername());
            commerceDetailRes.setPhone(httpMemberRes.getMobilePhone());
        }
        return RestResult.success(commerceDetailRes);
    }


}
